package com.yzt.minemodule.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.arouter.RouterAccount;
import com.yzt.zhuju.arouter.RouterMine;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.dialog.MenuSelectDialog;
import com.yzt.zhuju.event.RxBus;
import com.yzt.zhuju.event.main.ExitAppEvent;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.home.UserInfoBean;
import com.yzt.zhuju.utils.AppDialogUtil;
import com.yzt.minemodule.R;
import com.yzt.p_base.utils.ToastUtil;
import com.yzt.plugin_network.response.HttpResponse;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 账号安全
 */
@Route(path = RouterMine.ACTIVITY_ACCOUNT_SECURITY_ACTIVITY)
public class AccountSecurityActivity extends BaseActivity {

    private TextView phone;

    public static void start(Activity activity) {
        activity.startActivity(new Intent(activity, AccountSecurityActivity.class));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_account_security;
    }

    @Override
    public void installViews() {
        mTitleBar.setTitle("账号与安全");
        phone = findViewById(R.id.tv_phone);

        // 更换号码
        findViewById(R.id.tv_face_bind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterAccount.ResetPhoneActivity();
                finish();
            }
        });

        // 注销账户
        findViewById(R.id.tv_remove).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MenuSelectDialog menuSelectDialog = new MenuSelectDialog(AccountSecurityActivity.this, R.style.ActionSheetDialogStyle);
                menuSelectDialog.setOnItemClickListener(i1 -> {
                    AppDialogUtil.showTitleDialog(mActivity, getString(R.string.del_account),
                            mActivity.getString(R.string.cancellation_tip),
                            mActivity.getString(R.string.sure),
                            mActivity.getString(R.string.cancel),
                            new AppDialogUtil.DialogClickInterface() {
                                @Override
                                public void onCancelClick() {
                                    BizLayer.getInstance().getAccountModule().logoff(RXingUtils.getUserId(), new AresSubscriber<HttpResponse>() {
                                        @Override
                                        protected void onSuccess(HttpResponse httpResponse) {
                                            ToastUtil.showMessage(getString(R.string.logo_off_success));
                                            RxBus.getDefault().post(new ExitAppEvent());
                                            mActivity.finish();
                                        }
                                    });
                                }

                                @Override
                                public void onSureClick() {

                                }
                            });
                });
            }
        });

        // 获取手机号
        getAccountBindInfo();
    }

    /**
     * 获取账号的绑定状态
     */
    private void getAccountBindInfo() {
        BizLayer.getInstance().getAccountModule().getUserFullInfo(RXingUtils.getUserId(), new AresSubscriber<UserInfoBean>() {
            @Override
            protected void onSuccess(UserInfoBean personalDetail) {
                phone.setText(personalDetail.getPhone());
            }
        });
    }
}